package com.xy.p2p.action.admin;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.p2p.model.Tuser;
import com.xy.p2p.model.Twidrapp;
import com.xy.p2p.model.Tyedtransferout;
import com.xy.p2p.service.TyedtransferoutService;



@Controller
public class TyedtransferoutAction{
	private static final Logger log = LoggerFactory.getLogger(TyedtransferoutAction.class);
	public TyedtransferoutAction() {
	}
 
 
 	/**
	 * 余额贷转出申请表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tyedtransferoutAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tyedtransferout", new Tyedtransferout());
        setDict(model);
		return "tyedtransferoutEdit";
	}

	/**
	 * 余额贷转出申请表新增保存
	 * 
	 * @param tyedtransferout
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("o_tyedtransferoutSave.do")
	public String save(Tyedtransferout tyedtransferout, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tyedtransferout, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tyedtransferout = tyedtransferoutService.save(tyedtransferout);
		return "redirect:v_tyedtransferoutList.do";
	}

	/**
	 * 余额贷转出申请表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tyedtransferoutDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tyedtransferoutService.remove(id);
			}
		}
		return "redirect:v_tyedtransferoutList.do";
	}

	/**
	 * 余额贷转出申请表编辑页面
	 * 
	 * @param toid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tyedtransferoutEdit.do")
	public String edit(long toid, HttpServletRequest request, ModelMap model) {
		Tyedtransferout tyedtransferout = tyedtransferoutService.get(toid);
		model.addAttribute("tyedtransferout", tyedtransferout);
        setDict(model);
		return "tyedtransferoutEdit";
	}
	@RequestMapping("v_tyedtransferoutAudit.do")
	public String audit(long toid, HttpServletRequest request, ModelMap model) {
		Tyedtransferout tyedtransferout = tyedtransferoutService.get(toid);
		model.addAttribute("tyedtransferout", tyedtransferout);
		model.addAttribute("opr", "audit");
        setDict(model);
		return "tyedtransferoutView";
	}
	@RequestMapping("v_tyedtransferoutAuditSubmit.do")
	public String auditSubmit(long toid,String opr, HttpServletRequest request, ModelMap model) {
		Tyedtransferout tyedtransferout = tyedtransferoutService.get(toid);
		tyedtransferoutService.audit((Tuser)request.getSession().getAttribute(TuserAction.AUTH_KEY), opr, tyedtransferout);
		return "redirect:v_tyedtransferoutList.do";
	}
	/**
	 * 余额贷转出申请表详情
	 * 
	 * @param toid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tyedtransferoutDetail.do")
	public String detail(long toid, HttpServletRequest request, ModelMap model) {
		Tyedtransferout tyedtransferout = tyedtransferoutService.get(toid);
		model.addAttribute("tyedtransferout", tyedtransferout);
        setDict(model);
		return "tyedtransferoutView";
	}

	/**
	 * 余额贷转出申请表查询（分页）
	 * 
	 * @param tyedtransferout
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tyedtransferoutList.do")
	public String listPage(Tyedtransferout tyedtransferout, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tyedtransferoutService.getTyedtransferoutByPage(toMap(tyedtransferout),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tyedtransferoutList";
	}

	/**
	 * 余额贷转出申请表查询
	 * 
	 * @param tyedtransferout
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tyedtransferoutList1.do")
	public String list(Tyedtransferout tyedtransferout, HttpServletRequest request, ModelMap model) {
		List<Tyedtransferout> list = tyedtransferoutService.getTyedtransferout(toMap(tyedtransferout));
		model.addAttribute("list", list);
        setDict(model);
		return "tyedtransferoutList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tyedtransferout
	 * @return
	 */	private Map<String,Object> toMap(Tyedtransferout tyedtransferout){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tyedtransferout.getToid()!=null&&!tyedtransferout.getToid().equals("")){
			map.put("toid", tyedtransferout.getToid());
		}
		if(tyedtransferout.getTmember()!=null&&!tyedtransferout.getTmember().equals("")){
			map.put("Tmember", tyedtransferout.getTmember());
		}
		if(tyedtransferout.getToam()!=null&&!tyedtransferout.getToam().equals("")){
			map.put("toam", tyedtransferout.getToam());
		}
		if(tyedtransferout.getToamdate()!=null&&!tyedtransferout.getToamdate().equals("")){
			map.put("toamdate", tyedtransferout.getToamdate());
		}
		if(tyedtransferout.getTuser()!=null&&!tyedtransferout.getTuser().equals("")){
			map.put("Tuser", tyedtransferout.getTuser());
		}
		if(tyedtransferout.getRevieweddate()!=null&&!tyedtransferout.getRevieweddate().equals("")){
			map.put("revieweddate", tyedtransferout.getRevieweddate());
		}
		if(tyedtransferout.getOperator()!=null&&!tyedtransferout.getOperator().equals("")){
			map.put("operator", tyedtransferout.getOperator());
		}
		if(tyedtransferout.getOperatedate()!=null&&!tyedtransferout.getOperatedate().equals("")){
			map.put("operatedate", tyedtransferout.getOperatedate());
		}
		if(tyedtransferout.getStatus()!=null&&!tyedtransferout.getStatus().equals("")){
			map.put("status", tyedtransferout.getStatus());
		}
		if(tyedtransferout.getRemark()!=null&&!tyedtransferout.getRemark().equals("")){
			map.put("remark", tyedtransferout.getRemark());
		}
		if(tyedtransferout.getTtenderrecord()!=null&&!tyedtransferout.getTtenderrecord().equals("")){
			map.put("Ttenderrecord", tyedtransferout.getTtenderrecord());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TYEDTRANSFEROUT_STATUS));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tyedtransferout tyedtransferout, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TyedtransferoutService tyedtransferoutService;   
}